﻿using SWBetongXaydung.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SWBetongXaydung.BLL
{
    class NhanNhienLieuBLL
    {
        public static List<NhanNhienLieu> GetList(int thang = 0, int nam = 0, BETONGXAYDUNGEntities dbContext = null)
        {
            BETONGXAYDUNGEntities db = null;
            if (dbContext != null)
            {
                db = dbContext;
            }
            else
                db = DBContextHelper.GetContext();
            if (thang == 0)
            {
                var list = (from e in db.NhanNhienLieuDbSet
                            orderby e.NgayNhan, e.TaiXe.Ten, e.TaiXe.HoDem descending
                            select e).ToList();
                if (dbContext == null)
                    db.Dispose();
                return list;
            }
            else
            {
                var list = (from e in db.NhanNhienLieuDbSet
                            where e.NgayNhan.Month == thang && e.NgayNhan.Year == nam
                            orderby e.NgayNhan, e.TaiXe.Ten, e.TaiXe.HoDem descending
                            select e).ToList();
                if (dbContext == null)
                    db.Dispose();
                return list;
            }
        }

        public static CODE_RESULT_RETURN Add(NhanNhienLieu nhanNhienLieu, BETONGXAYDUNGEntities dbContext = null)
        {
            BETONGXAYDUNGEntities db = null;
            if (dbContext != null)
            {
                db = dbContext;
            }
            else
                db = DBContextHelper.GetContext();
            db.NhanNhienLieuDbSet.Add(nhanNhienLieu);
            db.SaveChanges();
            if (dbContext == null)
                db.Dispose();
            return CODE_RESULT_RETURN.ThanhCong;
        }

        public static CODE_RESULT_RETURN Update(NhanNhienLieu nhanNhienLieu, BETONGXAYDUNGEntities dbContext = null)
        {
            BETONGXAYDUNGEntities db = null;
            if (dbContext != null)
            {
                db = dbContext;
            }
            else
                db = DBContextHelper.GetContext();
            var obj = db.NhanNhienLieuDbSet.Where(e => e.IDNhanNhienLieu == nhanNhienLieu.IDNhanNhienLieu).SingleOrDefault();
            if (obj != null)
            {
                obj.NgayNhan = nhanNhienLieu.NgayNhan;
                obj.SoXe = nhanNhienLieu.SoXe;
                obj.IDTaiXe = nhanNhienLieu.IDTaiXe;
                obj.LuongNhienLieu = nhanNhienLieu.LuongNhienLieu;
                obj.TinhTrang = nhanNhienLieu.TinhTrang;
                db.SaveChanges();
                if (dbContext == null)
                    db.Dispose();
                return CODE_RESULT_RETURN.ThanhCong;
            }
            else
            {
                if (dbContext == null)
                    db.Dispose();
                return CODE_RESULT_RETURN.ThatBai;
            }
        }

        public static CODE_RESULT_RETURN Delete(int IDNhanNhienLieu, BETONGXAYDUNGEntities dbContext = null)
        {
            BETONGXAYDUNGEntities db = null;
            if (dbContext != null)
            {
                db = dbContext;
            }
            else
                db = DBContextHelper.GetContext();
            var obj = db.NhanNhienLieuDbSet.Where(e => e.IDNhanNhienLieu == IDNhanNhienLieu).SingleOrDefault();
            if (obj != null)
            {
                db.NhanNhienLieuDbSet.Remove(obj);
                db.SaveChanges();
                if (dbContext == null)
                    db.Dispose();
                return CODE_RESULT_RETURN.ThanhCong;
            }
            else
            {
                if (dbContext == null)
                    db.Dispose();
                return CODE_RESULT_RETURN.ThatBai;
            }
        }
    }
}
